package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.translateappjava.Activity.HomeFragmentActivity.BadResultFragment;
import com.example.translateappjava.Activity.HomeFragmentActivity.GoodResultFragment;
import com.example.translateappjava.R;
import com.example.translateappjava.Utils.ImageData;
import com.example.translateappjava.Utils.ImageDataLite;

import java.io.ByteArrayOutputStream; // 添加这一行
import java.util.ArrayList;

public class ResultActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private ResultPagerAdapter adapter;
    private TextView noRecognitionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        // 获取 Intent 传递的 ImageDataLite 列表
        ArrayList<ImageDataLite> imageDataLiteList = getIntent().getParcelableArrayListExtra("imageDataLiteList");

        // 获取控件
        viewPager = findViewById(R.id.viewPager);
        noRecognitionText = findViewById(R.id.noRecognitionText);

        // 如果没有有效数据，则显示提示文字
        if (imageDataLiteList == null || imageDataLiteList.isEmpty()) {
            noRecognitionText.setVisibility(View.VISIBLE);
        } else {
            noRecognitionText.setVisibility(View.GONE);
        }

        adapter = new ResultPagerAdapter(this, imageDataLiteList);
        viewPager.setAdapter(adapter);
    }

    private static class ResultPagerAdapter extends FragmentStateAdapter {
        private final ArrayList<ImageDataLite> imageDataLiteList;

        public ResultPagerAdapter(@NonNull FragmentActivity fa, ArrayList<ImageDataLite> imageDataLiteList) {
            super(fa);
            this.imageDataLiteList = imageDataLiteList;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            ImageDataLite data = imageDataLiteList.get(position);
            String imageUrl = data.getImageURL();
            String suggestion = data.getSuggestion();
            String recognitionResult = data.getRecognitionResult();

            // 根据 eatable 属性选择合适的 Fragment
            if (data.isEatable()) {
                return GoodResultFragment.newInstance(imageUrl, suggestion, recognitionResult);
            } else {
                return BadResultFragment.newInstance(imageUrl, suggestion, recognitionResult);
            }
        }

        @Override
        public int getItemCount() {
            return imageDataLiteList.size();
        }
    }
}




